home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / FILES.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  8KB  |  344 lines

  1.  
  2. /********************************************
  3. files.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log:    files.c,v $
  14.  * Revision 3.4.1.1  91/09/14  17:23:17  brennan
  15.  * VERSION 1.0
  16.  * 
  17.  * Revision 3.4  91/08/13  06:51:23  brennan
  18.  * VERSION .9994
  19.  * 
  20.  * Revision 3.3  91/06/28  04:16:39  brennan
  21.  * VERSION 0.999
  22.  * 
  23.  * Revision 3.2  91/06/10  15:59:11  brennan
  24.  * changes for V7
  25.  * 
  26.  * Revision 3.1  91/06/07  10:27:27  brennan
  27.  * VERSION 0.995
  28.  * 
  29.  * Revision 2.4  91/06/06  09:42:03  brennan
  30.  * added HAVE_FCNTL
  31.  * 
  32.  * Revision 2.3  91/05/16  12:19:44  brennan
  33.  * cleanup of machine dependencies
  34.  * 
  35.  * Revision 2.2  91/05/06  15:00:59  brennan
  36.  * flush output before fork
  37.  * 
  38.  * Revision 2.1  91/04/08  08:23:05  brennan
  39.  * VERSION 0.97
  40.  * 
  41. */
  42.  
  43. /* files.c */
  44.  
  45. #include "mawk.h"
  46. #include "files.h"
  47. #include "memory.h"
  48. #include "fin.h"
  49.  
  50.  
  51. #ifdef  V7
  52. #include  <sgtty.h>    /* defines FIOCLEX */
  53. #endif
  54.   
  55.  
  56. #if  HAVE_FCNTL_H  
  57.  
  58. #include <fcntl.h>
  59. #define  CLOSE_ON_EXEC(fd)   (void) fcntl(fd, F_SETFD, 1)
  60.  
  61. #else
  62. #define  CLOSE_ON_EXEC(fd) ioctl(fd, FIOCLEX, (PTR) 0)
  63. #endif
  64.  
  65.  
  66. /* We store dynamically created files on a linked linear
  67.    list with move to the front (big surprise)  */
  68.  
  69. typedef struct file {
  70. struct file *link ;
  71. STRING  *name ;
  72. short type ;
  73. #if   ! MSDOS
  74. #ifndef THINK_C
  75. int pid ;  /* we need to wait() when we close an out pipe */
  76. #endif
  77. #endif
  78. PTR   ptr ;  /* FIN*   or  FILE*   */
  79. }  FILE_NODE ;
  80.  
  81. static FILE_NODE *file_list ;
  82.  
  83. PTR  file_find( sval, type )
  84.   STRING *sval ;
  85.   int type ;
  86. { register FILE_NODE *p = file_list ;
  87.   FILE_NODE *q = (FILE_NODE *) 0 ;
  88.   char *name = sval->str ;
  89.  
  90.   while (1)
  91.   {
  92.     if ( !p )   /* open a new one */
  93.     {
  94.       p = (FILE_NODE*) zmalloc(sizeof(FILE_NODE)) ;
  95.       switch( p->type = type )
  96.       {
  97.         case  F_TRUNC :
  98.             if ( !(p->ptr = (PTR) fopen(name, "w")) )
  99.                 goto out_failure ;
  100.             break ;
  101.  
  102.         case  F_APPEND :
  103.             if ( !(p->ptr = (PTR) fopen(name, "a")) )
  104.                 goto out_failure ;
  105.             break ;
  106.  
  107.         case  F_IN  :
  108.             if ( !(p->ptr = (PTR) FINopen(name, 0)) )
  109.             { zfree(p, sizeof(FILE_NODE)) ; return (PTR) 0 ; }
  110.             break ;
  111.  
  112.         case  PIPE_OUT :
  113.         case  PIPE_IN :
  114. #if   MSDOS
  115.             rt_error("pipes not supported under MSDOS") ;
  116. #else
  117. #ifdef THINK_C
  118.             rt_error("pipes not supported on the Macintosh Toy Operating System") ;
  119. #else
  120.             if ( !(p->ptr = get_pipe(name, type, &p->pid)) )
  121.                 if ( type == PIPE_OUT ) goto out_failure ;
  122.                 else
  123.                 { zfree(p, sizeof(FILE_NODE) ) ;
  124.                   return (PTR) 0 ;
  125.                 }
  126. #endif
  127. #endif
  128.             break ;
  129.  
  130. #ifdef  DEBUG
  131.         default :
  132.             bozo("bad file type") ;
  133. #endif
  134.       }
  135.       /* successful open */
  136.       p->name = sval ;
  137.       sval->ref_cnt++ ;
  138.       break ;
  139.     }
  140.  
  141.     if ( strcmp(name, p->name->str) == 0 )
  142.     { 
  143.       if ( p->type != type )  goto type_failure ;
  144.       if ( !q )  /*at front of list */
  145.           return  p->ptr ;
  146.       /* delete from list for move to front */
  147.       q->link = p->link ;
  148.       break ;
  149.     }
  150.     q = p ; p = p->link ;
  151.   }
  152.  
  153.   /* put p at the front of the list */
  154.   p->link = file_list ;
  155.   return  (PTR) (file_list = p)->ptr ;
  156.  
  157. out_failure:
  158.   errmsg(errno, "cannot open \"%s\" for output", name) ;
  159.   mawk_exit(1) ;
  160.  
  161. type_failure :
  162.   rt_error("use of file \"%s\"\n\tis inconsistent with previous use",
  163.            name) ;
  164. }
  165.  
  166.  
  167. /* close a file and delete it's node from the file_list */
  168.  
  169. int  file_close( sval )
  170.   STRING *sval ;
  171. { register FILE_NODE *p = file_list ;
  172.   FILE_NODE *q = (FILE_NODE *) 0 ; /* trails p */
  173.   char *name = sval->str ;
  174.  
  175.   while ( p )
  176.         if ( strcmp(name,p->name->str) == 0 ) /* found */
  177.         { 
  178.           switch( p->type )
  179.           {
  180.             case  F_TRUNC :
  181.             case  F_APPEND :    
  182.                 (void) fclose((FILE *) p->ptr) ;
  183.                 break ;
  184.  
  185.             case  PIPE_OUT :
  186.                 (void) fclose((FILE *) p->ptr) ;
  187. #if  ! MSDOS
  188. #ifndef THINK_C
  189.                 (void) wait_for(p->pid) ;
  190. #endif
  191. #endif
  192.                 break ;
  193.  
  194.             case F_IN  :
  195.             case PIPE_IN :
  196.                 FINclose((FIN *) p->ptr) ;
  197.                 break ;
  198.           }
  199.  
  200.           free_STRING(p->name) ;
  201.           if ( q )  q->link = p->link ;
  202.           else  file_list = p->link ;
  203.  
  204.           zfree(p, sizeof(FILE_NODE)) ;
  205.           return 0 ;
  206.         }
  207.         else { q = p ; p = p->link ; }
  208.  
  209.   /* its not on the list */
  210.   return -1 ;
  211. }
  212.  
  213. /* When we exit, we need to close and wait for all output pipes */
  214.  
  215. #if   !MSDOS
  216. #ifndef THINK_C
  217. void close_out_pipes()
  218. { register FILE_NODE *p = file_list ;
  219.  
  220.   while ( p )
  221.   { if ( p->type == PIPE_OUT )
  222.     { (void) fclose((FILE *) p->ptr) ;  (void) wait_for(p->pid) ; }
  223.     p = p->link ;
  224.   }
  225. }
  226. #endif
  227. #endif
  228.  
  229. /* hardwire to /bin/sh for portability of programs */
  230. char *shell = "/bin/sh" ;
  231.  
  232. #if  !  MSDOS
  233. #ifndef THINK_C
  234. PTR get_pipe( name, type, pid_ptr)
  235.   char *name ;
  236.   int type ;
  237.   int *pid_ptr ;
  238. { int the_pipe[2], local_fd, remote_fd ;
  239.  
  240.   if ( pipe(the_pipe) == -1 )  return (PTR) 0 ;
  241.   local_fd = the_pipe[type == PIPE_OUT] ;
  242.   remote_fd = the_pipe[type == PIPE_IN ] ;
  243.   /* to keep output ordered correctly */
  244.   fflush(stdout) ; fflush(stderr) ;
  245.  
  246.   switch( *pid_ptr = fork() )
  247.   { case -1 :  
  248.       (void) close(local_fd) ;
  249.       (void) close(remote_fd) ;
  250.       return (PTR) 0 ;
  251.  
  252.     case  0 :
  253.         (void) close(local_fd) ;
  254.         (void) close(type == PIPE_IN) ;
  255.         (void) dup( remote_fd ) ;
  256.         (void) close( remote_fd ) ;
  257.         (void) execl(shell, shell, "-c", name, (char *) 0 ) ;
  258.         errmsg(errno, "failed to exec %s -c %s" , shell, name) ;
  259.         fflush(stderr) ;
  260.         _exit(128) ;
  261.  
  262.     default :
  263.         (void) close(remote_fd) ;
  264.         /* we could deadlock if future child inherit the local fd ,
  265.            set close on exec flag */
  266.         CLOSE_ON_EXEC(local_fd) ;
  267.         break ;
  268.   }
  269.  
  270.   return  type == PIPE_IN ? (PTR) FINdopen(local_fd, 0) : 
  271.                             (PTR)  fdopen(local_fd, "w")  ;
  272. }
  273.   
  274.  
  275.  
  276. /*------------ children ------------------*/
  277.  
  278. /* we need to wait for children at the end of output pipes to
  279.    complete so we know any files they have created are complete */
  280.  
  281. /* dead children are kept on this list */
  282.  
  283. static struct child {
  284. int pid ;
  285. int exit_status ;
  286. struct child *link ;
  287. }  *child_list ;
  288.  
  289. static  void  add_to_child_list(pid, exit_status)
  290.   int pid, exit_status ;
  291. { register struct child *p = 
  292.           (struct child *) zmalloc(sizeof(struct child)) ;
  293.  
  294.   p->pid = pid ; p->exit_status = exit_status ;
  295.   p->link = child_list ; child_list = p ;
  296. }
  297.  
  298. static struct child *remove_from_child_list(pid)
  299.   int pid ;
  300. { register struct child *p = child_list ;
  301.   struct child *q = (struct child *) 0 ;
  302.  
  303.   while ( p )
  304.     if ( p->pid == pid )
  305.     {
  306.         if ( q ) q->link = p->link ;
  307.         else child_list = p->link ;
  308.         break ;
  309.     }
  310.     else { q = p ; p = p->link ; }
  311.  
  312.   return p ;  /* null return if not in the list */
  313. }
  314.     
  315.  
  316. /* wait for a specific child to complete and return its 
  317.    exit status */
  318.  
  319. int wait_for(pid)
  320.   int pid ;
  321. { int exit_status ;
  322.   struct child *p ;
  323.   int id ;
  324.  
  325.   /* see if an earlier wait() caught our child */
  326.   if ( p = remove_from_child_list(pid) ) 
  327.   { exit_status = p->exit_status ;
  328.     zfree(p, sizeof(struct child)) ;
  329.   }
  330.   else /* need to really wait */
  331.     while ( (id = wait(&exit_status)) != pid )
  332.         if ( id == -1 ) /* can't happen */  bozo("wait_for") ;
  333.         else
  334.         { /* we got the exit status of another child
  335.              put it on the child list and try again */
  336.           add_to_child_list(id, exit_status ) ;
  337.         }
  338.  
  339.   return exit_status ;
  340. }
  341.         
  342. #endif
  343. #endif
  344.